function appendToken(request) {
    var t = getName(tokenName);
    if (t != null ) {
        request.setRequestHeader("token", getName(tokenName));
    }
}

function contestInfo(contestId) {
    $.ajax({
        type: "GET",
        url : ojurl + "/contest/info/main",
        data: {"contestId": contestId},
        timeout: 10000,
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showInfo(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
}

function contestProblemList(contestId) {
    $.ajax({
        type: "GET",
        url : ojurl + "/contest/list/problem",
        data: {"contestId": contestId},
        timeout: 10000,
        beforeSend: function (request) {
            appendToken(request)
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showProblemList(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
}

function problemInfo(contestId, problemId) {
    $.ajax({
        type: "GET",
        url : ojurl + "/contest/info/problem",
        data: {"contestId": contestId, "problemId": problemId},
        timeout: 10000,
        beforeSend: function (request) {
            appendToken(request)
        },
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showProblemInfo(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
}

function submitCode(jsonData) {
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + "/code/submitCode",
        dataType: "json",
        contentType: "application/json",
        beforeSend: function (request) {
            appendToken(request)
        },
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                window.location.href = "status?contestId="+contestId+"&problemId="+problemId;
            }
            else if (result.code == HttpStatus.UNAUTHORIZED) {
                removeName(tokenName);
                removeName(localName);
                alert(result.message);
                window.location.reload();
            }
            else {
                alert(result.message);
            }
        }
    })
}

function status(jsonData, contestId) {
    var subUrl = "/contest/statusSearch";
    if (jsonData.problemId == null && jsonData.name == null && jsonData.lang == null && jsonData.state == null) {
        subUrl = "/contest/status";
    }
    jsonData.size = String(statusPageSize);
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + subUrl,
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showStatus(result.data, contestId);
            }
            else {
                alert(result.message);
            }
        }
    })
}

function showCode(solutionId, contestId) {
    var jsonData = new Object();
    jsonData.solutionId = String(solutionId);
    jsonData.isContest = "1";
    jsonData.contestId = String(contestId);
    var data = new Object();
    data.data = jsonData;
    $.ajax({
        type: "POST",
        url: ojurl + "/code/downloadCode",
        dataType: "json",
        contentType: "application/json",
        beforeSend: function (request) {
            appendToken(request)
        },
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                var code = result.data.code;
                document.getElementById("myModalLabel").innerHTML = "solutionId: " + String(solutionId);
                document.getElementById("showCode").value = code;
                $("#myModal").modal("show");
            }
            else if (result.code == HttpStatus.UNAUTHORIZED) {
                removeName(tokenName);
                removeName(localName);
                alert("user login token expired!");
                window.location.reload();
            }
            else {
                alert(result.message);
            }
        }
    })
}

function rankList(contestId) {
    $.ajax({
        type : "GET",
        url  : ojurl + "/contest/rank",
        data : {"contestId": contestId},
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                showRankList(result.data);
            }
            else {
                alert(result.message);
            }
        }
    })
} 

function getVerifyCode(obj) {
    $.ajax ({
        type: "GET",
        url: ojurl + "/common/verifyCode",
        dataType: "json",
        timeout: 10000,
        success: function(result) {
            document.getElementById(obj).src = "data:image/jpeg;base64," + result.img;
            sessionStorage.setItem("uuid", result.uuid);
        }
    });
}
function login(obj, data) {
    $.ajax ({
        type: "POST",
        url: ojurl + "/contest/login",
        dataType: "json",
        contentType: "application/json",
        timeout: 10000,
        data: JSON.stringify(data),
        success: function(result) {
            if (result.code == HttpStatus.SUCCESS) {
                resultData = result.data;
                setName(tokenName, resultData.token);
                setName(localName, resultData.name);
                var url = "problem?contestId="+contestId;
                window.location.href = url;
            }
            else {
                document.getElementById(obj).textContent = result.message;
            }
        }
    });
}